<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
interface Post {
  id: number
  summary: string
  writer: string
  updateTime: string
  likeNum: number
  commentNum: number
}

const posts = ref<Post[]>([])
const loading = ref(true)

// 模拟数据
const mockPosts = [
  {
    id: 1,
    summary: "Vue 3 新特性详解：Composition API 的使用指南",
    writer: "技术达人",
    updateTime: "2024-01-15",
    likeNum: 156,
    commentNum: 23
  },
  {
    id: 2,
    summary: "前端开发中的性能优化技巧分享",
    writer: "前端工程师",
    updateTime: "2024-01-14",
    likeNum: 89,
    commentNum: 15
  },
  {
    id: 3,
    summary: "TypeScript 高级类型系统深度解析",
    writer: "TS专家",
    updateTime: "2024-01-13",
    likeNum: 234,
    commentNum: 45
  }
]

onMounted(() => {
  setTimeout(() => {
    posts.value = mockPosts
    loading.value = false
  }, 1000)
})

const goToPost = (id: number) => {
  router.push(`/posts/${id}`)
}
</script>

<template>
  <div class="home">
    <!-- 热门帖子 -->
    <div class="section">
      <h2>热门帖子</h2>
      <div class="posts-list">
        <el-card v-for="post in posts" :key="post.id" class="post-row" @click="goToPost(post.id)">
          <div class="post-row-content">
            <span class="post-title">{{ post.summary }}</span>
            <span class="post-meta">
              <span class="post-author"><el-icon><User /></el-icon>{{ post.writer }}</span>
              <span class="post-time"><el-icon><Clock /></el-icon>{{ post.updateTime }}</span>
              <el-button text size="small" @click.stop="goToPost(post.id)">
                <el-icon><View /></el-icon>详情
              </el-button>
            </span>
          </div>
        </el-card>
      </div>
    </div>

    <!-- 热门商品 -->
    <div class="section">
      <h2>热门商品</h2>
      <div class="posts-list">
        <el-card class="post-row" @click="router.push('/posts')">
          <div class="post-row-content">
            <span class="post-title">浏览帖子</span>
            <span class="post-meta">
              <span class="post-author"><el-icon><Document /></el-icon>帖子</span>
              <el-button text size="small" @click.stop="router.push('/posts')">
                <el-icon><View /></el-icon>进入
              </el-button>
            </span>
          </div>
        </el-card>
        <el-card class="post-row" @click="router.push('/create-post')">
          <div class="post-row-content">
            <span class="post-title">发布帖子</span>
            <span class="post-meta">
              <span class="post-author"><el-icon><Edit /></el-icon>发帖</span>
              <el-button text size="small" @click.stop="router.push('/create-post')">
                <el-icon><View /></el-icon>进入
              </el-button>
            </span>
          </div>
        </el-card>
        <el-card class="post-row" @click="router.push('/market')">
          <div class="post-row-content">
            <span class="post-title">跳蚤市场</span>
            <span class="post-meta">
              <span class="post-author"><el-icon><Shop /></el-icon>二手</span>
              <el-button text size="small" @click.stop="router.push('/market')">
                <el-icon><View /></el-icon>进入
              </el-button>
            </span>
          </div>
        </el-card>
        <el-card class="post-row" @click="router.push('/login')">
          <div class="post-row-content">
            <span class="post-title">登录/注册</span>
            <span class="post-meta">
              <span class="post-author"><el-icon><Key /></el-icon>账号</span>
              <el-button text size="small" @click.stop="router.push('/login')">
                <el-icon><View /></el-icon>进入
              </el-button>
            </span>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<style scoped>
.home {
  width: 100%;
  margin: 0 auto;
  box-sizing: border-box;
  padding: 0;
}

.welcome-banner {
  margin-bottom: 30px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.banner-content {
  text-align: center;
  padding: 40px 20px;
}

.banner-content h1 {
  margin: 0 0 10px 0;
  font-size: 2.5em;
  font-weight: bold;
}

.banner-content p {
  margin: 0 0 20px 0;
  font-size: 1.2em;
  opacity: 0.9;
}

.section {
  margin-bottom: 32px;
}

.section h2 {
  color: #333;
  margin-bottom: 16px;
  font-size: 1.2em;
  border-left: 4px solid #409eff;
  padding-left: 12px;
  text-align: left;
}

.posts-list {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 24px;
}

.el-card.post-row {
  width: 95% !important;
  margin: 0 auto !important;
  max-width: none !important;
  box-sizing: border-box;
  padding: 0 10px;
}

.post-row {
  cursor: pointer;
  transition: box-shadow 0.2s;
  padding: 8px 8px;
  font-size: 0.96em;
  background: #fff;
}

.post-row:hover {
  box-shadow: 0 4px 20px rgba(0,0,0,0.08);
}

.post-row-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.post-title {
  flex: 1 1 0;
  text-align: left;
  font-weight: 500;
  color: #222;
  font-size: 0.98em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-right: 12px;
}

.post-meta {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.9em;
  color: #888;
  flex-shrink: 0;
}

.post-author, .post-time {
  display: flex;
  align-items: center;
  gap: 3px;
}

@media (max-width: 968px) {
  .home {
    padding: 0 10px;
  }
  
  .section h2 {
    font-size: 0.5em;
  }
  
  .post-row {
    padding: 10px 15px;
  }
  
  .post-title {
    font-size: 0.9em;
  }
  
  .post-meta {
    font-size: 0.8em;
  }
}
</style>